package com.gewu.pm.ui.activity.customer;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.androidkun.xtablayout.XTabLayout;
import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.ui.fragment.salesperson.SalespersonDeliveryFragment;
import com.gewu.pm.ui.fragment.salesperson.SalespersonTerminalFragment;
import com.gewu.pm.ui.fragment.salesperson.SalespersonVisitFragment;
import com.hjq.base.FragmentPagerAdapter;

/**
 * 业务员详情页面
 * 
 * 功能：
 * 1. Tab切换（配送商、终端、拜访）
 * 2. 顶部显示业务员名称
 * 3. 支持搜索配送商/终端
 * 4. 显示拜访记录
 */
public final class SalesPersonDetailsActivity extends AppActivity {

    private XTabLayout mTabLayout;
    private ViewPager mViewPager;
    private TextView mSalespersonNameView;
    private TextView mSalespersonCodeView;
    private TextView mSalesAreaView;
    private TextView mDepartmentView;
    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;

    /**
     * 启动业务员详情页面
     * 
     * @param context 上下文
     * @param salespersonName 业务员名称
     */
    public static void start(Context context, String salespersonName) {
        Intent intent = new Intent(context, SalesPersonDetailsActivity.class);
        intent.putExtra(IntentKey.NAME, salespersonName);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.salesperson_details_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mTabLayout = findViewById(R.id.tab_layout);
        mViewPager = findViewById(R.id.view_pager);
        mSalespersonNameView = findViewById(R.id.tv_salesperson_name);
        mSalespersonCodeView = findViewById(R.id.tv_salesperson_code);
        mSalesAreaView = findViewById(R.id.tv_sales_area);
        mDepartmentView = findViewById(R.id.tv_department);
    }

    @Override
    protected void initData() {
        // 获取传递的业务员名称
        String salespersonName = getString(IntentKey.NAME);
        if (salespersonName != null && !salespersonName.isEmpty()) {
            mSalespersonNameView.setText(salespersonName);
        }

        // 初始化 FragmentPagerAdapter
        mPagerAdapter = new FragmentPagerAdapter<>(this);
        mPagerAdapter.addFragment(SalespersonDeliveryFragment.newInstance(), getString(R.string.salesperson_delivery_tab));
        mPagerAdapter.addFragment(SalespersonTerminalFragment.newInstance(), getString(R.string.salesperson_terminal_tab));
        mPagerAdapter.addFragment(SalespersonVisitFragment.newInstance(), getString(R.string.salesperson_visit_tab));
        mViewPager.setAdapter(mPagerAdapter);
        
        // 关联 XTabLayout 和 ViewPager
        mTabLayout.setupWithViewPager(mViewPager);
    }
}